/**
 * Copyright 2011 Pete Cornish.
 * 
 * This file is part of log4droid.
 * 
 * log4droid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * log4droid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with log4droid.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.log4droid;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

/**
 * @author pete
 *
 */
public class Logger {
	
	private static final int MAX_TAG_LENGTH = 23;
	
    private static Map<String, Logger> loggers;
	private String loggerName;
    private String messagePrefix;
	
	static {
		loggers = Collections.synchronizedMap(new HashMap<String, Logger>());
	}
	
	/**
     * @param loggerName
	 */
	private Logger(String loggerName) {
	    this.messagePrefix = (Configuration.getPrefixFullClassName() ? loggerName + ": " : "");
		this.loggerName = trimTag(loggerName);
	}

	/**
	 * @param tag
	 * @return tag shortened to {@link #MAX_TAG_LENGTH}
	 */
	private String trimTag(String tag) {
        if (tag.length() <= MAX_TAG_LENGTH) {
            return tag;
        } else {
            return tag.substring(tag.length() - MAX_TAG_LENGTH);
        }
    }

    /**
	 * @param clazz
	 * @return
	 */
	public static Logger getLogger(Class<?> clazz) {
		//initialise first
		Configuration.initialise();
		
		final String className = getClassName(clazz);
		
		//get or create logger
		Logger logger = loggers.get(className);
		if (null == logger) {
			logger = new Logger(className);
			loggers.put(className, logger);
		}
		return logger;
	}

	/**
	 * @param clazz
	 * @return
	 */
	protected static String getClassName(Class<?> clazz) {
		if (Configuration.isShowPackage()) {
			return clazz.getCanonicalName();
		} else {
			return clazz.getSimpleName();
		}
	}
	
	/**
	 * @param logLevel - a logging level from {@link Log}
	 * @return
	 */
	public boolean isLoggable(int logLevel) {
	    return Log.isLoggable(loggerName, logLevel);
	}
	
	/**
	 * @param msg
	 */
	public void debug(String msg) {
		Log.d(loggerName, messagePrefix + msg);
	}
	
	/**
	 * @param msg
	 */
	public void info(String msg) {
		Log.i(loggerName, messagePrefix + msg);
	}
	
	/**
	 * @param msg
	 */
	public void warn(String msg) {
		Log.w(loggerName, messagePrefix + msg);
	}
	
	/**
	 * @param msg
	 * @param t
	 */
	public void warn(String msg, Throwable t) {
		Log.w(loggerName, messagePrefix + msg, t);
	}

	/**
	 * @param msg
	 */
	public void error(String msg) {
		Log.e(loggerName, messagePrefix + msg);
	}
	
	/**
	 * @param msg
	 * @param t
	 */
	public void error(String msg, Throwable t) {
		Log.e(loggerName, messagePrefix + msg, t);
	}
}
